﻿using System;
using System.Web.UI.WebControls;

using System.Data.SqlClient;

namespace IFD2
{
    public partial class FltExpiredOrders : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("IA2") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                lblAirline.Text = Session["AIRLINE"].ToString();
                if (Session["FLT_NO"] != null)
                    txtFltNo.Text = Session["FLT_NO"].ToString();
                if (Session["FLT_DATE"] != null)
                    txtFltDate.Text = Convert.ToDateTime(Session["FLT_DATE"].ToString()).ToShortDateString();

                if (Request.QueryString["flt_no"] != null && Request.QueryString["flt_date"] != null)
                {
                    txtFltNo.Text = Request.QueryString["flt_no"].ToString();
                    txtFltDate.Text = Convert.ToDateTime(Request.QueryString["flt_date"].ToString()).ToShortDateString();
                }
            }
            lblError.Text = "";
            errCancel.Text = "";
            btnCancel.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime.Parse(txtFltDate.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                txtFltDate.Text = "";
                return;
            }
            Session["FLT_DATE"] = txtFltDate.Text;
            Session["FLT_NO"] = txtFltNo.Text;
            GridView1.DataBind();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("IA4") < 0)
            {
                errCancel.Text = "Unauthorized!";
                return;
            }

            int ret_code;
            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            bool meet_error = false;
            lblError.Text = "";
            cmd.CommandText = "bll_bk_cancel_trip";
            CheckBox cbox;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                cbox = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("select");
                if (cbox.Checked)
                {
                    cmd.Parameters.Clear();
                    para = cmd.Parameters.AddWithValue("@bk_id", GridView1.Rows[i].Cells[1].Text);
                    para = cmd.Parameters.AddWithValue("@trip_sn", GridView1.Rows[i].Cells[11].Text);
                    para = cmd.Parameters.AddWithValue("@action", "C");
                    para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                    para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                    para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                    para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                    try
                    {
                        ret_code = TinyDAL.Exec(ref cmd);
                        if (ret_code != 0)
                            meet_error = true;
                        switch (ret_code)
                        {
                            case 0:
                                //errCancel.Text = "Done!";
                                break;
                            case 1:
                                errCancel.Text = "Order or trip not found!";
                                break;
                            case 2:
                                errCancel.Text = "Update trip failed!";
                                break;
                            case 3:
                                errCancel.Text = "Order in waiting queue can not be timeout!";
                                break;
                            case 4:
                                errCancel.Text = "Update flt cabin failed!";
                                break;
                            case 5:
                                errCancel.Text = "Update flt class failed!";
                                break;
                            case 150:
                                errCancel.Text = "Re-calculate class failed!";
                                break;
                            default:
                                errCancel.Text = "Unknow error:" + ret_code.ToString();
                                break;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (Application["DEBUG"].ToString() == "1")
                            errCancel.Text = ex.Message;
                        else
                            errCancel.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
                        return;
                    }
                }
                if (meet_error)
                    break;
            }
            if (!meet_error)
                errCancel.Text = "Done";
            GridView1.DataBind();
        }

        protected void btnSelectAll_Click(object sender, EventArgs e)
        {
            CheckBox cbox;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                cbox = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("select");
                cbox.Checked = true;
            }
        }

        protected void btnSelectNone_Click(object sender, EventArgs e)
        {
            CheckBox cbox;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                cbox = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("select");
                cbox.Checked = false;
            }
        }
    }
}